<template>
  <div class="logDiv">
    <el-row>
      <el-col>
        <!-- 分界线-->
        <el-divider>
          <span style=" font-size: 25px; font-weight: bold;color: #67C23A;">美丽乡村旅游网用户登录</span>
        </el-divider>
        <el-form :model="ruleForm" ref="ruleForm" class="login-box" :rules="rules">
          <el-form-item prop="username">
            <el-input prefix-icon="el-icon-user" placeholder="请输入账号" v-model="ruleForm.username" autocomplete="off"
              clearable>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-unlock" placeholder="请输入密码" type="password" v-model="ruleForm.password"
              autocomplete="off" show-password clearable></el-input>
          </el-form-item>
          <el-form-item prop="checkCode">
            <div id="codestyle">
              <el-input prefix-icon="el-icon-menu" placeholder="请输入验证码" v-model="ruleForm.checkCode"
                style="width: 150px;padding-right: 10px;" @keyup.enter.native="confirm"></el-input>
              <img :src=ruleForm.codeUrl style="width: 100px;height: 40px;" />
              <el-button type="success" @click="newCode" class="new" style="margin-left: 10px;">点击刷新</el-button>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="confirm">登录</el-button>
          </el-form-item>

          <!-- 底部按键-->
          <el-form-item>
            <el-button type="text" size="small" style="float: left; " @click="back">取消</el-button>
            <el-button type="text" @click="register" size="small" style="float: right;">立即注册</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
// import { ApiService } from "@/services";
import newAxios from "@/utils/axios.js";

export default {
  name: "loginUser",
  data() {
    return {
      ruleForm: {
        username: "",
        password: "",
        codeUrl: "",
        checkCode: ""
      },
      rules: {
        username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        checkCode: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
      },
      confirm_disabled: false,
      formVisible: true,
    };
  },
  created() {
    this.getCode()
  },
  methods: {
    //跳转至注册页面
    register() {
      console.log("用户跳转至注册页面");
      this.$router.push({ path: "/register" });
    },

    back() {
      this.$router.go(-1);
    },
    getCode() {
      newAxios.get("/getCode/lyf").then((res) => {
        console.log(res.data.data);
         if(res.data.code==200){
          this.ruleForm.codeUrl = res.data.data;
         }else{
           this.$message.error("验证码获取失败");
         }
      });
    },
    newCode() {
      newAxios.get("/getCode/" + new Date().getMilliseconds()).then((res) => {
        console.log(res.data.data);
        if(res.data.code==200){
          this.ruleForm.codeUrl = res.data.data;
         }else{
           this.$message.error("验证码获取失败");
         }
      });
    },
    confirm() {
      console.log("账号：",this.ruleForm.username=='');
      if(this.ruleForm.username ==''){
        this.getCode();
        this.$message({
              message: '用户名不能为空！',
              type: 'error'
            });
      }else if(this.ruleForm.password ==''){
        this.getCode();
        this.$message({
              message: '密码不能为空！',
              type: 'error'
            });
      }else{
        newAxios
        .post("/user/login", {
          ...this.ruleForm,
        })
        .then((res) => {
          console.log(res);
          // console.log(res.data);
          if (res.data.code == 200) {
            let data = res.data.data;
            console.log(data);
            sessionStorage.setItem("token", data.token);
            sessionStorage.setItem("id", data.adminid);
            //跳转到主页
            // this.$router.push("/layout/homeTravel");
            this.$router.replace("/homepage");
            location.reload();
            this.$message({
              message: '登录成功！',
              type: 'success'
            });
          } else if (res.data.code == 500) {
            this.confirm_disabled = false;
            this.getCode();
            this.$message({
              message: '验证码已过期！',
              type: 'error'
            });
          }
          else if (res.data.code == 600) {
            this.confirm_disabled = false;
            this.getCode();
            this.$message({
              message: '验证码错误！',
              type: 'error'
            });
          }
          else {
            this.confirm_disabled = false;
            this.getCode();
            this.$message.error(res.data.msg);
          }
        });
      }
    },
  },
};
</script>

<style scoped>
.login-box {
  border: 1px solid #dcdfe6;
  width: 350px;
  margin: 0 auto;
  margin-top: 100px;
  padding: 50px 50px 10px 50px;
  border-radius: 5px;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  box-shadow: 0 0 25px #909399;
}

#codestyle {
  display: flex;

}
</style>
